package com.goodving.guli.view;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.androidquery.util.AQUtility;

/**
 * Created by Administrator on 2015-11-16.
 */
public class CircleMask extends View implements ValueAnimator.AnimatorUpdateListener {
    public static float CIRCLE_MASK_WIDTH;
    public static float SMALL_CIRCLE_MASK_WIDTH;
    public static String TAG;
    private Point centerPoint_;
    int cricleWdithPx_;
    private int currentRadian;
    public CircleMaskShapeHolder currentRadianSH;
    private ObjectAnimator objectAnimator_;
    private Paint paint_;
    private Path path_;
    float radius;
    private RectF rectFInside_;
    private RectF rectFOutside_;
    private RectF rectFSmallDown_;
    private RectF rectFSmallUp_;
    int smallCircleWidthPx_;
    float smallRadius;

    class CircleMaskShapeHolder {
        private int currentRadian;

        public void setCurrentRadian(int i) {
            this.currentRadian = i;
        }

        public int getCurrentRadian() {
            return this.currentRadian;
        }

        public CircleMaskShapeHolder(int i) {
            this.currentRadian = 0;
            this.currentRadian = i;
        }
    }

    static {
        TAG = CircleMask.class.getName();
        CIRCLE_MASK_WIDTH = 206.0f;
        SMALL_CIRCLE_MASK_WIDTH = 23.0f;
    }

    public CircleMask(Context context) {
        super(context);
        this.currentRadianSH = new CircleMaskShapeHolder(0);
        this.cricleWdithPx_ = 0;
        this.smallCircleWidthPx_ = 0;
        this.radius = 0.0f;
        this.smallRadius = 0.0f;
        this.currentRadian = 0;
        this.cricleWdithPx_ = AQUtility.dip2pixel(context, CIRCLE_MASK_WIDTH);
        this.smallCircleWidthPx_ = AQUtility.dip2pixel(context, SMALL_CIRCLE_MASK_WIDTH);
        this.centerPoint_ = new Point(this.cricleWdithPx_ / 2, this.cricleWdithPx_ / 2);
        this.path_ = new Path();
        this.paint_ = new Paint();
        this.paint_.setAntiAlias(true);
        this.paint_.setColor(-1);
        this.radius = (float) (this.cricleWdithPx_ / 2);
        this.rectFOutside_ = new RectF(((float) this.centerPoint_.x) - this.radius, ((float) this.centerPoint_.y) - this.radius, ((float) this.centerPoint_.x) + this.radius, ((float) this.centerPoint_.y) + this.radius);
        this.rectFInside_ = new RectF((((float) this.centerPoint_.x) - this.radius) + ((float) this.smallCircleWidthPx_), (((float) this.centerPoint_.y) - this.radius) + ((float) this.smallCircleWidthPx_), (((float) this.centerPoint_.x) + this.radius) - ((float) this.smallCircleWidthPx_), (((float) this.centerPoint_.y) + this.radius) - ((float) this.smallCircleWidthPx_));
        this.smallRadius = (float) (this.smallCircleWidthPx_ / 2);
        this.rectFSmallUp_ = new RectF(((float) this.centerPoint_.x) - this.smallRadius, ((float) this.centerPoint_.y) - this.radius, ((float) this.centerPoint_.x) + this.smallRadius, (((float) this.centerPoint_.y) - this.radius) + ((float) this.smallCircleWidthPx_));
        this.rectFSmallDown_ = new RectF();

    }

    private void createAnimator(int i, int i2) {
        if (this.objectAnimator_ == null) {
            this.objectAnimator_ = ObjectAnimator.ofInt(this.currentRadianSH, "currentRadian", new int[]{i, i2}).setDuration(700);
            this.objectAnimator_.addUpdateListener(this);
            this.objectAnimator_.setInterpolator(new AccelerateDecelerateInterpolator());
            return;
        }
        this.objectAnimator_.setIntValues(new int[]{i, i2});
    }

    public void updateCircleMask(int i, int i2, boolean z) {
        if (z) {
//            C1472m.m12013a(TAG, "start: " + i + " end: " + i2);
            createAnimator(i, i2);
            this.objectAnimator_.start();
            return;
        }
        this.currentRadianSH.setCurrentRadian(i2);
        invalidate();
    }

    protected void onDraw(Canvas canvas) {
        canvas.save();
        this.currentRadian = this.currentRadianSH.getCurrentRadian();
        if (this.currentRadian < 350) {
            this.path_.reset();
            if (this.currentRadian == 0) {
                this.paint_.setStyle(Paint.Style.STROKE);
                this.paint_.setStrokeWidth((float) this.smallCircleWidthPx_);
                canvas.drawCircle((float) this.centerPoint_.x, (float) this.centerPoint_.y, this.radius - this.smallRadius, this.paint_);
                return;
            }
            this.paint_.setStyle(Paint.Style.FILL);
            this.path_.arcTo(this.rectFOutside_, (float) (this.currentRadian - 90), (float) (360 - this.currentRadian));
            this.path_.arcTo(this.rectFSmallUp_, -90.0f, -180.0f);
            this.path_.arcTo(this.rectFInside_, -90.0f, (float) ((360 - this.currentRadian) * -1));
            float sin = (float) (((double) this.centerPoint_.x) + (Math.sin(Math.toRadians((double) this.currentRadian)) * ((double) (this.radius - this.smallRadius))));
            float cos = (float) (((double) this.centerPoint_.y) - (Math.cos(Math.toRadians((double) this.currentRadian)) * ((double) (this.radius - this.smallRadius))));
            this.rectFSmallDown_.set(sin - this.smallRadius, cos - this.smallRadius, sin + this.smallRadius, cos + this.smallRadius);
            this.path_.arcTo(this.rectFSmallDown_, (float) (this.currentRadian + 90), -180.0f);
            canvas.drawPath(this.path_, this.paint_);
            canvas.restore();
        }
    }

    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        invalidate();
    }
}
